<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {
  var $location;
  var $address;
  var $addresses;
  var $locations;
  var $errs;

  function PageController() {
  	parent::__construct();
	$this->title = "Locations";
	$this->section = "admin";

	$action = $this->getParameter("action");
	$this->location = new Location();
	$this->address = new Address();
	$this->errs = array();

	$this->locations = LocationLogic::findAll();
	$this->addresses = AddressLogic::findAll();

	switch (strtoupper($action))
	{
	  case "SAVE" :
		  $this->parseForm();
		  if (!$this->validate()) return;

		  if (strlen($this->address->line1) > 0 ||
		  	  strlen($this->address->zip) > 0 ||
		  	  strlen($this->address->state) > 0)
		  {
		  	 AddressLogic::save($this->address);
		  	 $this->addresses = AddressLogic::findAll();
		  }
		  $this->location->address_id = $this->address->id;
		  LocationLogic::save($this->location);

		  $this->addMessage("Saved");

		  $this->locations = LocationLogic::findAll();
		  break;

	  case "LOAD" :
		  $id = $this->getParameter("id");
		  $this->location = LocationLogic::load($id);
		  $this->address = AddressLogic::load($this->location->address_id);
		  break;
	  default :
	}
  }

  function parseForm() {
	$this->location->readForm();
	$this->address->readForm();
	$this->address->id = $this->getParameter("address_id");
  }

  function validate() {
	return (sizeof($this->errs) == 0);
  }

}



?>
